<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-30
 * Time: 13:55
 * table store 门店管理
 *
 * -1--->'StoreManage'--->门店(storeId)
 *                           ||
 *                      storemanage
 *                      门店信息（id,   storeName, province, city, county, address,    floors）
     *                         storeId    门店名称       省       市    区     详细地址  楼层[]
     *                          string    string      string   string string   string   array
 */

namespace app\index\controller;


class Store
{
    const storemanage = [
        'storeName'=>'',    //门店名
        'province'=>'',     //省
        'city'=>'',         //市
        'county'=>'',       //区
        'address'=>'',      //地址
        'floors'=>[],       //楼层
    ];

    public static function getData($where, $one = true, $table = 'storemanage')
    {
        return ($one == true) ? getFirst(mongo($table)->where($where)->select()) : mongo($table)->where($where)->select();
    }
    public static function setData($where, $data, $table = 'storemanage')
    {
        mongo($table)->where($where)->update($data);
    }
    public static function insertData($data, $table = 'storemanage')
    {
        mongo($table)->insert($data);
        return mongo($table)->getLastInsID();
    }
    public static function deleteData($where, $table = 'storemanage')
    {
        mongo($table)->where($where)->delete();
    }
    /**
     * 添加门店
     * @param string $storeName
     * @param string $province
     * @param string $city
     * @param string $county
     * @param string $address
     * @param array  $floors
     * @return array
     */
    public static function addStoreDb($storeName = null, $province = null, $city = null, $county = null, $address = null, $floors = null)
    {
        if(!empty($perssion = perssion([LOGIN])))
            return $perssion;

        $storeName   = input('storeName');
        $province    = input('province');
        $city        = input('city');
        $county      = input('county');
        $address     = input('address');
        $floors      = input('floors/a');
        if(empty($storeName))
            return RD(PARA_NOT_NULL);

        $insertData = [
            'storeName' =>$storeName,    //门店名
            'province'  =>$province,     //省
            'city'      =>$city,         //市
            'county'    =>$county,       //区
            'address'   =>$address,      //地址
            'floors'    =>$floors,       //楼层
        ];
        self::insertData(array_merge(self::storemanage, $insertData));
        return RD(SUCCESS, $insertData);
    }
    /**
     * 编辑门店
     * @param string $storeId  门店id
     * @param string $storeName
     * @param string $province
     * @param string $city
     * @param string $county
     * @param string $address
     * @param array  $floors
     * @return array
     */
    public  static function editStoreDb($storeId = null, $storeName = null, $province = null, $city = null, $county = null, $address = null, $floors = null)
    {
        if(!empty($perssion = perssion([LOGIN])))
            return $perssion;

        $storeId     = input('storeId');
        $storeName   = input('storeName');
        $province    = input('province');
        $city        = input('city');
        $county      = input('county');
        $address     = input('address');
        $floors      = input('floors/a');

        if(empty($storeId))
            return RD(PARA_NOT_NULL);
        $updateData = [
            'storeName' =>$storeName,    //门店名
            'province'  =>$province,     //省
            'city'      =>$city,         //市
            'county'    =>$county,       //区
            'address'   =>$address,      //地址
            'floors'    =>$floors,       //楼层
        ];
        self::setData(['id'=>$storeId], $updateData);
        return RD(SUCCESS);
    }

    /**
     * 获取门店
     * @param string $currentPage
     * @param string $pageSize
     * @param array $keyword     关键字 省 市 区
     * @return array
     */
    public  static function getStoresDb($currentPage = null, $pageSize = null, $keyword = null)
    {
        $currentPage = input('currentPage');
        $pageSize    = input('pageSize');
        $keyword     = input('keyword/a');

        $where = [
            'province'=>['like', '.*'.($keyword[0] ?? '').'.*'],
            'city'=>['like', '.*'.($keyword[1] ?? '').'.*'],
            'county'=>['like', '.*'.($keyword[2] ?? '').'.*'],
        ];
        $user = User::getData(['id'=>cookie('userId')]);
        //管理员权限
        if(($user['rankName'] ?? '') != '管理员' && !empty($user['rankName'] ?? '')) {
            $where['id'] = $user['store_id']?? '';
        }
        $data = self::getData($where, false);
        $count = count($data);
        if($pageSize > 0) {
            array_paging($data, ($currentPage - 1)* $pageSize, $pageSize);
        }

        return RD(SUCCESS, ["total"=>$count,  "stores"=>$data]);
    }

    /**
     * 删除门店id
     * @param string $storeId 门店id
     * @return array
     */
    public static function deleteStoreDb($storeId = null)
    {
        if(!empty($perssion = perssion([LOGIN])))
            return $perssion;
        if(empty($vars = getInputAll('storeId')))
            return RD(PARA_NOT_NULL);

        self::deleteData(['id'=>$vars['storeId']]);      //删除门店

        EquipRecord::deleteByStore($storeId);            //删除专业
        Malfunction::deleteData(['storeId'=>$storeId]);  //删除故障报修
        EnergyStore::deleteData(['storeId'=>$storeId]);  //删除抄表商户
        EnergyReadMeter::deleteByStore($storeId);                 //删除抄表计划
        return RD(SUCCESS);
    }
    /**
     * 获取门店楼层
     * @param string $storeId 门店id
     * @return array
     */
    public static function getFloors($storeId = null)
    {
        if(!empty($perssion = perssion([LOGIN])))
            return $perssion;

        $storeId = User::getStoreId();
        $floors = self::getData(['id'=>$storeId])['floors'] ?? [];

        return RD(SUCCESS, $floors);
    }
}